package cn.edu.njust.control;

import cn.edu.njust.pojo.User;
import cn.edu.njust.service.ChangeService;
import cn.edu.njust.service.impl.ChangeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "changeServlet", value = "/changeServlet")
public class ChangeServlet extends HttpServlet {
    public ChangeService cl = new ChangeServiceImpl();


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json;charset=UTF-8");

        String email = request.getParameter("email");
        String telephone = request.getParameter("telephone");
        String pw01 = request.getParameter("pw01");
        String pw02 = request.getParameter("pw02");
        String pw03 = request.getParameter("pw03");

        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        String id = loginUser.getIdNo();


        User Changeuser = new User();
        Changeuser.setEmail(email);
        Changeuser.setTelephone(telephone);
        Changeuser.setPassword(pw02);

        if (pw02 == pw03) {
            System.out.println("123");
            request.setAttribute("login_msg", "验证码错误！");
            request.getRequestDispatcher("changeInfo.jsp").forward(request, response);
        }
        else {
            cl.changeUser(Changeuser, id);
            response.sendRedirect("changeInfo.jsp");
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        this.doGet(request, response);
    }

}
